package thread;

public class WaitCorrespond {
    public static void main(String[] args) throws InterruptedException {
        Object o = new Object();
        Thread t1 = new Thread(new Worker(o));
        Thread t2 = new Thread(new Machine(o));
        t1.start();
        Thread.sleep(500l);
        t2.start();
    }

}
class Worker implements Runnable{
    public Object object;

    public Worker(Object object) {
        this.object = object;
    }

    @Override
    public void run() {
        this.m();
    }
    public void m(){
        synchronized (object){
            System.out.println(Thread.currentThread().getName()+"工人开始工作了");
            try {
                object.wait();// 等待nofty通知
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"工人开始工作222");
        }
    }
}
class Machine implements Runnable{
    public Object object;

    public Machine(Object object) {
        this.object = object;
    }

    @Override
    public void run() {
        this.m();
    }
    public void m(){
        synchronized (object){
            System.out.println(Thread.currentThread().getName()+"机器人开始工作了");
            object.notify();//会把代码块执行完成
            System.out.println(Thread.currentThread().getName()+"notify");
        }
    }
}